package com.zhang.sparkstreaming_2

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Duration, StreamingContext}

/**
 * @title:
 * @author: zhang
 * @date: 2022/2/21 11:12 
 */
object SparkStreaming01_WordCount {

  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("spark streaming")
    val ssc = new StreamingContext(conf, Duration( 100L))

    //todo 从端口读取数据计算wordcount
    ssc
      .socketTextStream("localhost",9999)
      .flatMap(_.split(" ").map((_,1)))
      .reduceByKey(_+_)
      .print()


    //启动采集器
    ssc.start()
    ssc.awaitTermination()
  }
}
